﻿/* Project:         CmdParamLib
 * Copyright:       Copyright (C) 2010, Rob Kennedy
 * Create Date:     2010-14-08 4:00 PM
 * Modified Date:   2010-14-08 4:00 PM
 * Comments:        2010-14-08 4:00 PM RK  Ignore SA1633
 *
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>. 
 */
namespace CmdParamLib
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The command line interface class to retrieve parameters and values from a command line
    /// </summary>
    public class CommandLine
    {
        #region Private Properties
        /// <summary>
        /// Contains the parameters list
        /// </summary>
        private static List<CommandLineParameter> parameters;
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the executable parameter path from the command line arguments
        /// </summary>
        public static string ExecutablePath
        {
            get
            {
                return Environment.GetCommandLineArgs()[0];
            }
        }

        /// <summary>
        /// Gets a list collection of command line arguments and values from the command line
        /// </summary>
        public static List<CommandLineParameter> Parameters
        {
            get
            {
                parameters = new List<CommandLineParameter>();

                string[] arguments = Environment.GetCommandLineArgs();

                if (arguments != null && arguments.Length > 1)
                {
                    CommandLineParameter newParameter = new CommandLineParameter();

                    for (int index = 1; index < arguments.Length; index++)
                    {
                        string param = arguments[index];

                        if (param != null && param.Length > 1)
                        {
                            if (param[0] == '-')
                            {
                                if (newParameter.Name.Length == 0)
                                {
                                    newParameter.Name = param.Substring(1);
                                }
                                else
                                {
                                    // previous parameter already has a name, create a new parameter
                                    parameters.Add(newParameter);
                                    newParameter = new CommandLineParameter(param.Substring(1), null);
                                }
                            }
                            else 
                            {
                                // this is a value
                                newParameter.Value = param;
                            }
                        }
                    }

                    if (newParameter.Name.Length > 0)
                    {
                        parameters.Add(newParameter);
                    }
                }

                return parameters;
            }
        }
        #endregion
    }
}
